package com.imooc.newretail.bpl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imooc.newretail.bpl.entity.Role;
import com.imooc.newretail.bpl.entity.RolePermission;
import com.imooc.newretail.bpl.mapper.RoleMapper;
import com.imooc.newretail.bpl.service.IRolePermissionService;
import com.imooc.newretail.bpl.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 系统角色表 服务实现类
 * </p>
 *
 * @author Hank
 * @since 2020-06-27
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    IRolePermissionService permissionService;

    @Override
    @Transactional
    public boolean removeRoleAndPermission(String roleId) {
        QueryWrapper query = new QueryWrapper();
        query.eq("role_id", roleId);
        permissionService.remove(query);
        baseMapper.deleteById(roleId);
        return true;
    }

    public IPage<RolePermission> findPermissonByRoleId(String roleId, Page page) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id", roleId);
        return permissionService.page(page, queryWrapper);
    }

    @Transactional
    public boolean savePermissons(List<RolePermission> permissionList) {
        //先删除既有的？
        // TODO
        return permissionService.saveBatch(permissionList);
    }
}
